package com.ruoyi.system.userRegister.impl;

import com.alibaba.fastjson2.JSONArray;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.sign.Md5Utils;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.domain.UserInfo;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import com.ruoyi.system.userRegister.UserRegistrationService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
public class UserRegistrationServiceImpl implements UserRegistrationService {
    @Resource
    RedisCache redisCache;

    @Resource
    SysUserMapper sysUserMapper;

    @Resource
    SysUserRoleMapper sysUserRoleMapper;

    @Override
    public String MailRegister(String receiverEmail, String password, String number) {
        SysUser sysUser = sysUserMapper.selectUserByUserName(receiverEmail);
        if (sysUser != null) {
            return "该用户已注册！";
        }
        Integer getNumber = redisCache.getCacheObject(receiverEmail);
        String s = String.valueOf(getNumber);
        if (s.equals(number)) {
            Boolean isExist = redisCache.hasKey(receiverEmail);
            if (isExist) {
                return "您的填写的账号和之前发送邮件账号不一致！";
            }
            UserInfo userInfo = new UserInfo();
            userInfo.setUserName(receiverEmail);
            userInfo.setPassword(SecurityUtils.encryptPassword(password));
            userInfo.setEmail(receiverEmail);
            userInfo.setStatus(0);
            userInfo.setDelFlag(0);
            Date currentTime = new Date();
            userInfo.setCreate_time(currentTime);
            userInfo.setRemark("普通员工");
            int i = sysUserMapper.addUserInfo(userInfo);
            if (i < 0) {
                return "网络波动等原因，注册失败！请稍后重试。";
            }
            List<SysUserRole> list = new ArrayList<>();
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setUserId(userInfo.getUserId());
            sysUserRole.setRoleId(2L);
            list.add(sysUserRole);
            int isOk = sysUserRoleMapper.batchUserRole(list);
            if (isOk < 0) {
                sysUserMapper.deleteUserById(userInfo.getUserId());
                return "网络波动等原因，注册失败！请稍后重试。";
            }
            return "注册成功！";
        }
        return "验证码已失效！或者您输入的验证码不正确！";
    }

    @Override
    public String PasswordReset(String receiverEmail, String password, String number) {
        SysUser sysUser = sysUserMapper.selectUserByUserName(receiverEmail);
        if (sysUser == null) {
            return "该用户不存在！";
        }
        Integer getNumber = redisCache.getCacheObject(receiverEmail);
        String s = String.valueOf(getNumber);
        if (s.equals(number)) {
            Boolean isExist = redisCache.hasKey(receiverEmail);
            if (isExist) {
                return "您的填写的账号和之前发送邮件账号不一致！";
            }
            sysUser.setPassword(SecurityUtils.encryptPassword(password));
            int i = sysUserMapper.updateUser(sysUser);
            if (i<0){
                return "密码重置是失败！";
            }
            return "密码重置成功！";
        }
        return "验证码已失效！或者您输入的验证码不正确！";
    }
}
